package org.jotace.ganaralaonce;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final ImageButton cloverBtn = (ImageButton)findViewById(R.id.cloverBtn);
		cloverBtn.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					v.setBackgroundResource(R.drawable.clover_btn_pressed);
					
				}else if (event.getAction() == MotionEvent.ACTION_UP){
					v.setBackgroundResource(R.drawable.clover_btn);
				}
				return false;
			}
		});
		
		cloverBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.cash); 
				mp.start();
				
				final TextView digitOne = (TextView)findViewById(R.id.digitOne);
				int number = RandomUtils.generateRandomDigit();
				setDigitText(digitOne, number);
				
				final TextView digitTwo = (TextView)findViewById(R.id.digitTwo);
				number = RandomUtils.generateRandomDigit();
				setDigitText(digitTwo, number);
				
				final TextView digitThree = (TextView)findViewById(R.id.digitThree);
				number = RandomUtils.generateRandomDigit();
				setDigitText(digitThree, number);
				
				final TextView digitFour = (TextView)findViewById(R.id.digitFour);
				number = RandomUtils.generateRandomDigit();
				setDigitText(digitFour, number);
				
				final TextView digitFive = (TextView)findViewById(R.id.digitFive);
				number = RandomUtils.generateRandomDigit();
				setDigitText(digitFive, number);
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void setDigitText(TextView digit, int number) {
		
		switch (number) {
        case 0:  
        	digit.setText(R.string.zero);
            break;
        case 1:  
        	digit.setText(R.string.one);
            break;
        case 2:  
        	digit.setText(R.string.two);
            break;
        case 3:  
        	digit.setText(R.string.three);
            break;
        case 4:  
        	digit.setText(R.string.four);
            break;
        case 5:  
        	digit.setText(R.string.five);
            break;
        case 6:  
        	digit.setText(R.string.six);
            break;
        case 7:  
        	digit.setText(R.string.seven);
            break;
        case 8:  
        	digit.setText(R.string.eight);
            break;
        case 9:  
        	digit.setText(R.string.nine);
            break;
		}
		
	}


}
